package com.gsi.webstore.platform.storefront.pipelet.account;

import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.core.capi.pipeline.PipelineInitializationException;
import com.intershop.beehive.core.capi.profile.Address;
import com.intershop.beehive.core.capi.profile.Profile;
import com.intershop.beehive.core.internal.profile.BasicProfilePO;

/**
 * 
 */
public class SetDefaultAddress extends Pipelet
{

    /**
     * Constant used to access pipelet configuration with key 'DefaultAddress'
     */
    public static final String CN_DEFAULT_ADDRESS = "DefaultAddress";
    /**
     * Member attribute that holds the pipelet configuration value 'DefaultAddress'
     */
    private String cfg_defaultAddress = "";
    /**
     * Constant used to access pipelet configuration with key 'DefaultInstallToAddress'
     */
    public static final String CN_DEFAULT_INSTALL_TO_ADDRESS = "DefaultInstallToAddress";
    /**
     * Member attribute that holds the pipelet configuration value 'DefaultInstallToAddress'
     */
    private String cfg_defaultInstallToAddress = "";
    /**
     * Constant used to access pipelet configuration with key 'DefaultInvoiceToAddress'
     */
    public static final String CN_DEFAULT_INVOICE_TO_ADDRESS = "DefaultInvoiceToAddress";
    /**
     * Member attribute that holds the pipelet configuration value 'DefaultInvoiceToAddress'
     */
    private String cfg_defaultInvoiceToAddress = "";
    /**
     * Constant used to access pipelet configuration with key 'DefaultServiceToAddress'
     */
    public static final String CN_DEFAULT_SERVICE_TO_ADDRESS = "DefaultServiceToAddress";
    /**
     * Member attribute that holds the pipelet configuration value 'DefaultServiceToAddress'
     */
    private String cfg_defaultServiceToAddress = "";
    /**
     * Constant used to access pipelet configuration with key 'DefaultShipFromAddress'
     */
    public static final String CN_DEFAULT_SHIP_FROM_ADDRESS = "DefaultShipFromAddress";
    /**
     * Member attribute that holds the pipelet configuration value 'DefaultShipFromAddress'
     */
    private String cfg_defaultShipFromAddress = "";
    /**
     * Constant used to access pipelet configuration with key 'DefaultShipToAddress'
     */
    public static final String CN_DEFAULT_SHIP_TO_ADDRESS = "DefaultShipToAddress";
    /**
     * Member attribute that holds the pipelet configuration value 'DefaultShipToAddress'
     */
    private String cfg_defaultShipToAddress = "";
    /**
     * Constant used to access the pipeline dictionary with key 'Address'
     */
    public static final String DN_ADDRESS = "Address";
    /**
     * Constant used to access the pipeline dictionary with key 'Profile'
     */
    public static final String DN_PROFILE = "Profile";







    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict)
    throws PipeletExecutionException {        
        // lookup 'Profile' in pipeline dictionary
        Profile profile = (Profile)dict.get(DN_PROFILE);
        if (null == profile)
        {
            throw new PipeletExecutionException("Mandatory input parameter 'Profile' not available in pipeline dictionary.");
        }
    
        // lookup 'Address' in pipeline dictionary
        Address address = (Address)dict.get(DN_ADDRESS);
        if (null == address)
        {
            throw new PipeletExecutionException("Mandatory input parameter 'Address' not available in pipeline dictionary.");
        }

        if ("true".equals(this.cfg_defaultAddress))
        {
            profile.getAddressBook().setDefaultAddress(address);
        }
        if ("false".equals(this.cfg_defaultAddress))
        {
            if (profile.getAddressBook().isDefaultAddress(address))
            {
                BasicProfilePO profilePO = (BasicProfilePO)profile;
                profilePO.setDefaultAddressIDNull(true);
            }
        }

        if ("true".equals(this.cfg_defaultInstallToAddress))
        {
            profile.getAddressBook().setDefaultInstallToAddress(address);
        }
        if ("false".equals(this.cfg_defaultInstallToAddress))
        {
            if (profile.getAddressBook().isDefaultInstallToAddress(address))
            {
                BasicProfilePO profilePO = (BasicProfilePO)profile;
                profilePO.setDefaultInstallToAddressNull();
            }
        }

        if ("true".equals(this.cfg_defaultInvoiceToAddress))
        {
            profile.getAddressBook().setDefaultInvoiceToAddress(address);
        }
        if ("false".equals(this.cfg_defaultInvoiceToAddress))
        {
            if (profile.getAddressBook().isDefaultInvoiceToAddress(address))
            {
                BasicProfilePO profilePO = (BasicProfilePO)profile;
                profilePO.setDefaultInvoiceToAddressNull();
            }
        }

        if ("true".equals(this.cfg_defaultServiceToAddress))
        {
            profile.getAddressBook().setDefaultServiceToAddress(address);
        }
        if ("false".equals(this.cfg_defaultServiceToAddress))
        {
            if (profile.getAddressBook().isDefaultServiceToAddress(address))
            {
                BasicProfilePO profilePO = (BasicProfilePO)profile;
                profilePO.setDefaultServiceToAddressNull();
            }
        }

        if ("true".equals(this.cfg_defaultShipFromAddress))
        {
            profile.getAddressBook().setDefaultShipFromAddress(address);
        }
        if ("false".equals(this.cfg_defaultShipFromAddress))
        {
            if (profile.getAddressBook().isDefaultShipFromAddress(address))
            {
                BasicProfilePO profilePO = (BasicProfilePO)profile;
                profilePO.setDefaultShipFromAddressNull();
            }
        }

        if ("true".equals(this.cfg_defaultShipToAddress))
        {
            profile.getAddressBook().setDefaultShipToAddress(address);
        }
        if ("false".equals(this.cfg_defaultShipToAddress))
        {
            if (profile.getAddressBook().isDefaultShipToAddress(address))
            {
                BasicProfilePO profilePO = (BasicProfilePO)profile;
                profilePO.setDefaultShipToAddressNull();
            }
        }

        return PIPELET_NEXT;
    }

    /**
     * The pipelet's initialization method is called whenever the pipeline
     * used to read and process pipelet configuration values that are required
     * during the pipelet execution later on.
     *  
     * @throws  PipelineInitializationException
     *          Thrown if some error occured when reading the pipelet configuration.
     */
    public void init()
    {        
        // store 'DefaultAddress' config value in field variable
        cfg_defaultAddress = (String)getConfiguration().get(CN_DEFAULT_ADDRESS);

        // store 'DefaultInstallToAddress' config value in field variable
        cfg_defaultInstallToAddress = (String)getConfiguration().get(CN_DEFAULT_INSTALL_TO_ADDRESS);
        
        // store 'DefaultInvoiceToAddress' config value in field variable
        cfg_defaultInvoiceToAddress = (String)getConfiguration().get(CN_DEFAULT_INVOICE_TO_ADDRESS);
        
        // store 'DefaultServiceToAddress' config value in field variable
        cfg_defaultServiceToAddress = (String)getConfiguration().get(CN_DEFAULT_SERVICE_TO_ADDRESS);
        
        // store 'DefaultShipFromAddress' config value in field variable
        cfg_defaultShipFromAddress = (String)getConfiguration().get(CN_DEFAULT_SHIP_FROM_ADDRESS);

        // store 'DefaultShipToAddress' config value in field variable
        cfg_defaultShipToAddress = (String)getConfiguration().get(CN_DEFAULT_SHIP_TO_ADDRESS);
    }
}